package com.minhua.jijiehao2.http;


import com.minhua.jijiehao2.constants.HttpConst;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author moo
 * @date 2017/11/13.
 * @describe
 */

public class NoTokenApiClient {
    private static NoTokenApiClient mApiClient;
    private Retrofit mRetrofit;

    private NoTokenApiClient() {
        initRetrofit();
    }

    public static synchronized NoTokenApiClient getInstance() {
        if (mApiClient == null) {
            mApiClient = new NoTokenApiClient();
        }
        return mApiClient;
    }

    public ApiService getApi() {
        return mRetrofit.create(ApiService.class);
    }

    private void initRetrofit() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        builder.addInterceptor(new HttpLoggingInterceptor());
        builder.retryOnConnectionFailure(true);
        OkHttpClient client = builder.build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(HttpConst.HOST_NAME)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(client)
                .build();
    }
}
